package com.mock.ems.controller.config;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;

import java.util.Date;
import java.util.List;

/**
 * Created by MOCK on 2017/8/23.
 */
public class CloudCollegeController extends Controller {

    public void index() {

        Record config = Db.findFirst("select * from college_config");
        if (null != config) {
            setAttr("config",config.getColumns());
        }
        List<Record> recordList = Db.find("select * from college_module");
        if (CollectionUtil.isNotEmpty(recordList)) {
            recordList.forEach(item -> {
                item.set("create_time", DateUtil.format(item.getDate("create_time"),DateUtil.FULL_PATTERN));
            });
        }
        setAttr("cloudModuleList",recordList);
        renderJsp("/page/config/cloudcollege/cloud_college_list.jsp");
    }

    public void edit() {

        Long id = getParaToLong("id");
        if (null != id) {
            Record record = Db.findById("college_module",id);
            if (null != record) {
                setAttr("record",record.getColumns());
            }
        }
        renderJsp("/page/config/cloudcollege/cloud_college_edit.jsp");
    }

    @Before(Tx.class)
    public void save() {
        String title = getPara("title");
        String icon = getPara("icon");

        Long id = getParaToLong("id");
        Record moduleRecord = Db.findById("college_module",id);
        if (null == moduleRecord) {
            moduleRecord = new Record();
            moduleRecord.set("create_time",new Date());
            moduleRecord.set("status",0);
        }

        moduleRecord.set("title",title)
                .set("icon",icon)
                .set("content",getPara("content"))
                .set("update_time",new Date());

        if (null == id) {
            if (!Db.save("college_module",moduleRecord)) {
                renderJson(new JsonResult().status("-1").show("保存失败"));
                return;
            }
        }else {
            if (!Db.update("college_module",moduleRecord)) {
                renderJson(new JsonResult().status("-2").show("保存失败"));
                return;
            }
        }
        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    @Before(Tx.class)
    public void videoSave() {

        String videoUrl = getPara("video_url");
        if (StringUtil.isEmpty(videoUrl)) {
            renderJson(new JsonResult().status("-1"));
            return;
        }
        String sql = "update college_config set video_url = ?,video_image = ? ";
        if (Db.update(sql,videoUrl,getPara("video_image")) <= 0) {
            renderJson(new JsonResult().status("-2"));
        }else {
            renderJson(new JsonResult().status("000"));
        }
    }

    @Before(Tx.class)
    public void publish() {

        Long id = getParaToLong("id");
        if (null == id) {
            renderJson(new JsonResult().status("-1"));
            return;
        }
        if (Db.update("update college_module set status = 1") <= 0) {
            renderJson(new JsonResult().status("-2"));
            return;
        }
        renderJson(new JsonResult().status("000"));
    }
}
